<?php

namespace App\Http\Controllers\Home;

use App\Models\Follow;
use App\Models\Profile;
use App\Models\Remind;
use App\Models\Reply;
use App\Models\TopicBbs;
use App\Models\User;
use App\Models\Vote;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserCenterController extends Controller
{
    //用户中心
    public function user_view(Request $request,$id){
        $user=User::with('profile','topic','reply','another')->find($id);
        $followed=0;
        if ($request->session()->has('userModel')){

            if ($id==$request->session()->get('userModel')->id){
                return view('home/UserCenterViews/MyUserCenter',['user'=>$user]);
            }
            $my=User::with('follow')->find($request->session()->get('userModel')->id);
            foreach ($my->follow as $f){
                if ($f->another_id==$id){
                    $followed=1;
                }
            }
        }
        return view('home/UserCenterViews/UserCenter',['user'=>$user,'followed'=>$followed]);
    }
    //我的用户中心
    public function myUserCenter_view(Request $request){
        $user=User::with('profile','topic','reply')->find($request->session()->get('userModel')->id);
        return view('home/UserCenterViews/MyUserCenter',['user'=>$user]);
    }
    //用户资料更改界面
    public function profile_update_view(Request $request){
        $user=User::find($request->session()->get('userModel')->id);
        return view('home/UserCenterViews/User_update',['user'=>$user]);
    }
    //用户资料更改方法
    public function profile_update(Request $request){
        $updateData=$request->all();
        $user=User::find($request->session()->get('userModel')->id);
        $b=[];
        foreach ($updateData as $a){
            $b[]=$a;
        }

        if ($updateData['email']==$user['email']){
            if (!empty($b[1])){
                User::where('email',$updateData['email'])->update(['username'=>$b[1]]);
            };

            Profile::where('user_id',$user['id'])->update(['gender'=>$b[3]]);

            if (!empty($b[4])){
                Profile::where('user_id',$user['id'])->update(['real_name'=>$b[4]]);
            };
            if (!empty($b[5])){
                Profile::where('user_id',$user['id'])->update(['tel'=>$b[5]]);
            };
            if (!empty($b[6])){
                Profile::where('user_id',$user['id'])->update(['city'=>$b[6]]);
            };
            if (!empty($b[7])){
                Profile::where('user_id',$user['id'])->update(['signature'=>$b[7]]);
            };
            if (!empty($request->file('image'))){
                $file = $request->file('image');
                if ($file->getError()>0){
                    return redirect('profile_update')->with('red_msg','上传文件发生错误!');
                }
                if (!preg_match('/^[\x{4e00}-\x{9fa5}|\w|-|\.]+$/u', $file->getClientOriginalName())){
                    return redirect('profile_update')->with('red_msg','文件名不合法!');
                }
                if (!in_array($file->getClientMimeType(), ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'])){
                    return redirect('profile_update')->with('red_msg','您只能上传图片类型的文件!');
                }
                if ($file->getClientSize() > 5 * 1024 * 1024){
                    return redirect('profile_update')->with('red_msg','只能上传小于5M的图片!');
                }
                $destinationPath = 'home/img/avatar/';
                $extension = $file->getClientOriginalExtension();
                $fileName = str_random(10).'.'.$extension;
                $file->move($destinationPath, $fileName);
                $filePath = $destinationPath.$fileName;
                $old_avatar=Profile::where('user_id',$user['id'])->get()[0]->avatar;
                if (!empty($old_avatar) and file_exists($old_avatar)){
                    unlink($old_avatar);
                }

                Profile::where('user_id',$user['id'])->update(['avatar'=>$filePath]);
                $request->session()->get('userModel')->profile->avatar=$filePath;
                $user->profile['avatar']=$filePath;
            };
            return back()->with('green_msg','操作成功!');
        }
        echo '此操作需要用户登录';
    }
    //关注
    public function followUp(Request $request,$id){
            $another_id = $id;
            $user_id = $request->session()->get('userModel')->id;
            $follow = new Follow;
            $follow->user_id = $user_id;
            $follow->another_id = $another_id;
            $follow->save();
            $remind=new Remind;
            $remind->user_id=$user_id;
            $remind->another_id=$id;
            $remind->desc='follow';
            $remind->save();
            return back()->with('green_msg', '关注成功');
    }
    //取消关注
    public function followDown(Request $request,$id){
        $user_id=$request->session()->get('userModel')->id;
        Follow::where('user_id',$user_id)->where('another_id',$id)->delete();
        Remind::where('user_id',$user_id)->where('another_id',$id)->where('desc','follow')->delete();
        return back()->with('green_msg', '取关成功');
    }
    //Ta的关注
    public function Ta_follow(Request $request){
        $user_id=$request->input('Ta');
        $follower=Follow::where('user_id',$user_id)->with('another')->get();
        $top='Ta 关注的用户';
        return view('home/UserCenterViews/Ta',['user'=>$follower,'top'=>$top,'user_id'=>$user_id]);
    }
    //Ta的点赞
    public function Ta_vote(Request $request){
        $user_id=$request->input('Ta');
        $vote_topic=Vote::where('user_id',$user_id)->with('topic')->get();
        $top='Ta 点赞的文章';
        return view('home/UserCenterViews/Ta',['vote'=>$vote_topic,'top'=>$top,'user_id'=>$user_id]);
    }
    //Ta的评论
    public function Ta_reply(Request $request){
        $user_id=$request->input('Ta');
        $reply=Reply::where('user_id',$user_id)->with('topic','answer')->get();
        $top='Ta 发表的评论';
        return view('home/UserCenterViews/Ta',['reply'=>$reply,'top'=>$top,'user_id'=>$user_id]);
    }
    //Ta的文章
    public function Ta_topic(Request $request){
        $user_id=$request->input('Ta');
        $topic=TopicBbs::where('user_id',$user_id)->with('user','tags','replies','vote')->get();
        $top='Ta 发表的文章';
        return view('home/UserCenterViews/Ta',['topic'=>$topic,'top'=>$top,'user_id'=>$user_id]);
    }
    //Ta的关注者
    public function Ta_follower(Request $request){
        $user_id=$request->input('Ta');
        $follower=Follow::where('another_id',$user_id)->get();
        $top='Ta 的关注者';
        return view('home/UserCenterViews/Ta',['follower'=>$follower,'top'=>$top,'user_id'=>$user_id]);
    }
    //用户消息界面
    public function user_msg_view(Request $request){
        $user_id=$request->session()->get('userModel')->id;
        $remind=Remind::where('another_id',$user_id)->where('desc','answer')->orderBy('created_at','desc')->with('user','topic')->where('read',0)->get();
        return view('home/UserCenterViews/User_msg_view',['answer'=>$remind]);
    }
    //关注用户发表文章提醒
    public function msg_topic(Request $request){
        $user_id=$request->session()->get('userModel')->id;
        $follow=Follow::where('user_id',$user_id)->get();
        $a=[];
        foreach ($follow as $f){
            $a[]=$f->another->id;
        }
        $remind_topic=[];
        foreach ($a as $b){
            $remind_topic[]=Remind::where('user_id',$b)->where('desc','created_topic')->where('read',0)->orderBy('created_at','desc')->with('topic')->get();
        }
        $topic=[];
        foreach ($remind_topic as $a){
            foreach ($a as $b){
                $t=Follow::where('user_id',$user_id)->where('another_id',$b->user->id)->get();
                if ($b->created_at>$t[0]->created_at){
                    $topic[]=$b;
                }
            }
        }
        return view('home/UserCenterViews/msg_topic_view',['topic'=>$topic,'follow_id'=>$user_id]);
    }
    //ajax轮询
    public function user_msg(Request $request){
        date_default_timezone_set("PRC");
        set_time_limit(0);//无限请求超时时间
        while (true) {
            sleep(1);
            if ($request->session()->has('userModel')){
                $user_id=$request->session()->get('userModel')->id;
                $remind=Remind::where('another_id',$user_id)->where('read',0)->get();
                $i=count($remind);
                echo ($i);
                exit();
            }
            else {
                sleep(13);
                exit();
            }
        }
    }
    //个人动态界面
    public function reminds_view(Request $request){
        $user_id=$request->session()->get('userModel')->id;
        $remind_my=Remind::where('user_id',$user_id)->orderBy('created_at','desc')->get();
        return view('home/UserCenterViews/Trends_view',['remind_my'=>$remind_my]);
    }
    //已读与未读
    public function remind_read(Request $request){
        $remind_id=$request->input('remind_id');
        Remind::where('id',$remind_id)->update(['read'=>1]);
        return response()->json(array(
            'msg'=>'ok'
        ));
    }
}
